package com.open.capacity.sign.service.impl;

import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Daily;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;

import com.open.capacity.sign.dao.DailyDao;
import com.open.capacity.sign.service.DailyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class dailyServiceImpl implements DailyService {
    @Autowired
    private DailyDao dailyDao;

    /**
     * 日报提交
     * @param daily
     * @return
     */
    @Override
    public Result dailySubmitOrUpdate(Daily daily) {
        try {
            if(daily.getId() != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = dateFormat.format(new Date());
                daily.setUpdtim(time);
                Integer update = dailyDao.dailyUpdate(daily);
                return Result.succeed(update, "操作成功！");
            }else{
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time=dateFormat.format(new Date());
                daily.setCretim(time);
                Integer submit = dailyDao.dailySubmit(daily);
                return Result.succeed(submit, "操作成功！");
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public PageResult<Daily> dailyManage(Map<String, Object> params)  throws ServiceException {
        try {
            int total = dailyDao.count(params);
            List<Daily> list = Collections.emptyList();
            if (total > 0) {
                PageUtil.pageParamConver(params, true);
                list = dailyDao.findList(params);//查询所有日报信息

            }
            return PageResult.<Daily>builder().data(list)
                    .code(0).count((long) total).build();//收集所有数据
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public List<Daily> findAllDailys(Map<String, Object> params) throws ServiceException {
        try {
            List<Daily> dailyExcels = new ArrayList<>();
            List<Daily> list = dailyDao.findList(params);

            for (Daily daily : list) {
                Daily dailyExcel = new Daily();
                BeanUtils.copyProperties(daily, dailyExcel);
                dailyExcels.add(dailyExcel);
            }
            return dailyExcels;
        } catch (BeansException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Result dailyDelete(Daily daily) {
        try {
                Integer update = dailyDao.dailyDelete(daily);
                return Result.succeed(update, "删除成功！");
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
